package com.example.scanfast.receiver;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.BroadcastReceiver;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.util.Log;

import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat;

import com.example.scanfast.R;
import com.example.scanfast.constant.Constant;
import com.example.scanfast.utils.CommonUtil;

import java.util.Date;
import java.util.Random;

public class MyBroadcastReceiver extends BroadcastReceiver {
    private Context context;
    private ContentResolver contentResolver;

    private final String channel_id = "xulu";
    private final String channel_name = "myChannelName";
    private static final int DATABASE_QUERY_NOTIFICATION_ID = 1;
    private int importance = NotificationManagerCompat.IMPORTANCE_HIGH;

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.d(Constant.TAG, "定时任务执行了啊");
        CommonUtil.toastLong(context,"定时任务执行了啊");
        //CommonUtil.sendDatabaseQueryNotification(666,new Activity());

        sendMessage(context);
    }
    private void sendMessage(Context context) {
        // 获取通知管理器
        NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        if (notificationManager == null) {
            return;
        }

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            @SuppressLint("WrongConstant") NotificationChannel mChannel = new NotificationChannel(channel_id, channel_name, importance);
            mChannel.enableLights(true);
            mChannel.enableVibration(true);
            mChannel.setShowBadge(true);
            notificationManager.createNotificationChannel(mChannel);
        }
        // 使用文件路径的哈希值作为通知的唯一标识
        // 针对每个下载的文件都发送一个通知
        NotificationCompat.Builder builder = new NotificationCompat.Builder(context, channel_id)
                .setSmallIcon(R.mipmap.icon)
                .setAutoCancel(true)
                .setContentTitle("定时")
                .setContentText(CommonUtil.format(new Date(System.currentTimeMillis()))+"执行了定时任务啊")
                .setPriority(importance);
        int notificationId = generateNotificationId(); // 生成唯一的通知标识符

        notificationManager.notify(notificationId, builder.build());

    }
    private int generateNotificationId() {
        return (int) System.currentTimeMillis();
    }
}
